package com.xzxy.xmlg_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.xzxy.xmlg_backend.entity.Class;
import com.xzxy.xmlg_backend.entity.Result;
import com.xzxy.xmlg_backend.entity.Student;
import com.xzxy.xmlg_backend.entity.dto.ClassDto;
import com.xzxy.xmlg_backend.entity.vo.ClassVo;
import com.xzxy.xmlg_backend.service.IClassService;
import com.xzxy.xmlg_backend.service.IStudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/classes")
@Tag(name = "班级管理")
public class ClassController {

    @Autowired
    private IClassService clazzService;

    @Autowired
    private IStudentService studentService;

    /**
     * 获取班级列表
     * @return
     */
    @GetMapping
    @Operation(summary = "获取班级列表")
    public Result<List<Class>> getList() {
        return Result.success(clazzService.list());
    }

    /**
     * 新增班级
     * @param aClass
     * @return
     */
    @PostMapping
    @Operation(summary = "新增班级")
    public Result add(@RequestBody Class aClass) {
        aClass.setCreatedAt(LocalDateTime.now());
        aClass.setUpdatedAt(LocalDateTime.now());
        boolean b = clazzService.save(aClass);
        if (b) return Result.success();
        return Result.fail("新增失败");
    }

    /**
     * 更新班级
     * @param aClass
     * @return
     */
    @PutMapping
    @Operation(summary = "更新班级")
    public Result update(@RequestBody Class aClass) {
        aClass.setUpdatedAt(LocalDateTime.now());
        boolean b = clazzService.updateById(aClass);
        if (b) return Result.success();
        return Result.fail("更新失败");
    }


    /**
     * 删除班级
     * @param ids
     * @return
     */
//    @DeleteMapping("/{ids}")
//    @Operation(summary = "删除班级")
//    public Result delete(@PathVariable List<Long> ids) {
//        boolean b = clazzService.removeByIds(ids);
//        if (b) return Result.success();
//        return Result.fail("删除失败");
//    }
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除班级")
    public Result delete(@PathVariable List<Long> ids) {
        studentService.remove(new QueryWrapper<Student>().in("class_id", ids));

        // 再删除班级
        boolean b = clazzService.removeByIds(ids);
        return b ? Result.success() : Result.fail("删除失败");
    }

    /**
     * 根据id查询班级
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询班级")
    public Result<Class> getById(@PathVariable Integer id) {
        return Result.success(clazzService.getById(id));
    }

    /**
     * 分页查询
     * @param classDto
     * @return
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询",description = "分页查询信息")
    public Result page(ClassDto classDto){
        Page<Class> page = new Page<>(classDto.getPage(),classDto.getPageSize());
        LambdaQueryWrapper<Class> wrapper = new LambdaQueryWrapper<>();
        if (classDto.getName()!=null && classDto.getName()!="")
            wrapper.like(Class::getName,classDto.getName()).or().like(Class::getGrade,classDto.getName());
        ClassVo classVo = new ClassVo();
        classVo.setRows(clazzService.list(page,wrapper));
        classVo.setTotal(page.getTotal());
        return Result.success(classVo);
    }
}

